﻿using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCBloodPurification
{
    public partial class HistoryBP : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        public string Id = string.Empty;
        public string Num = string.Empty;
        public HistoryBP(PatientInfo patientInfo)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
        }
        //历史记录窗体加载
        private void HistoryBP_Load(object sender, EventArgs e)
        {
            var ret = formService.GetNursingFormRecordOrderByTime(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.BloodPurification, 1);
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("Num", typeof(string));
            dt.Columns.Add("AssDate", typeof(string));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    BloodPurification ldf = new BloodPurification();
                    ldf = XmlUtil.XMLToObject<BloodPurification>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["Id"] = record.Id;
                    row["PatientName"] = record.PatientName;
                    row["PatientId"] = record.PatientId;
                    row["Num"] = ldf.Num;
                    row["AssDate"] = Convert.ToDateTime(ldf.RiQi).ToString("yyyy-MM-dd");
                    dt.Rows.Add(row);
                }
                DataView dv = new DataView(dt);
                //合并后的数据按次数排序
                dv.Sort = "Num ASC";
                string[] columnsArray = { "Id", "PatientName", "PatientId", "Num","AssDate" };
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["Num"].ToString() == dt.Rows[i - 1]["Num"].ToString()) //去除相同次数的数据,只显示一条
                    {
                        dt.Rows.RemoveAt(i);
                        i--;
                    }
                }
                gridControl1.DataSource = dt;
            }
        }

        /// <summary>
        /// GridView表头双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            ConfirmOk();
        }

        private void ConfirmOk()
        {
            if (bandedGridView1.RowCount > 0)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                Id = id;
                string num = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Num").ToString();
                Num = num;
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }

        //确认按钮点击事件
        private void btnConfrim_Click(object sender, EventArgs e)
        {
            ConfirmOk();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// GridView单击行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
           ConfirmOk();
        }
    }
}
